#include "MenuState.hpp"
#include "../App.hpp"
#include "../ressource/ImageRessource.hpp"
#include "../ressource/MusicRessource.hpp"
#include "ProfilState.hpp"
#include "GameState.hpp"

MenuState::MenuState(App* appPtr) : AppState("MenuState", appPtr),
	m_music(NULL),
	_imageBackground(NULL),
	_imageNotHover(NULL)
{
	m_choicePlayer = MENU_CHOOSE_PLAY;
}

MenuState::~MenuState()
{

}

void MenuState::DoInit()
{
	// First : Call Init AppState
	AppState::DoInit();

	/* ****************************
	***		Load Ressource      ***
	*******************************
	*/


	// Load Menu Background
	_imageBackground = m_app->m_ressourceManager.AddImage("MENU_Background", "images/menu.png");

	// Load image resource Hover Light
	//_imageHoverLight = m_app->m_ressourceManager.AddImage("MENU_HoverLight", "images/light_hover_menu.png");
	//_imageHoverLight->Ressource()->CreateMaskFromColor(sf::Color(255, 0, 182));

	// Load image resource Not Hover
	_imageNotHover= m_app->m_ressourceManager.AddImage("MENU_NotHover", "images/dark_nothover_menu.png");
	_imageNotHover->Ressource()->CreateMaskFromColor(sf::Color(255, 0, 182));

	// Load Music
	m_music = m_app->m_ressourceManager.AddMusic("MENU_Music", "music/menu.ogg")->Ressource();

	// ******************************************

	//Sprite Menu Background
	m_spriteMenu.SetImage(*(_imageBackground->Ressource()));

	// Sprite Play
	m_spritePlay.SetImage(*(_imageNotHover->Ressource()));
	m_spritePlay.SetPosition(376, 84);
	m_spritePlay.SetColor(sf::Color(255, 255, 255, 120));

	// Sprite Option
	m_spriteOption.SetImage(*(_imageNotHover->Ressource()));
	m_spriteOption.SetPosition(376, 156);
	m_spriteOption.SetColor(sf::Color(255, 255, 255, 120));

	// Sprite Quit
	m_spriteQuit.SetImage(*(_imageNotHover->Ressource()));
	m_spriteQuit.SetPosition(376, 237);
	m_spriteQuit.SetColor(sf::Color(255, 255, 255, 120));

	// Play Music
	m_music->Play();

	// Initialize Choice Player
	m_choicePlayer = MENU_CHOOSE_PLAY;

}

void MenuState::ReInit()
{

}

void MenuState::Update(float elapsedTime)
{

}

void MenuState::HandleEvents(sf::Event appEvent)
{
	if(appEvent.Type == sf::Event::KeyPressed)
	{
		switch(appEvent.Key.Code)
		{
		case sf::Key::Up:
			IncrementChoice(REMOVE);
			break;
		case sf::Key::Down:
			IncrementChoice(ADD);
			break;
        case sf::Key::Space:
            if(m_choicePlayer == MENU_CHOOSE_PLAY)
            {
                m_app->m_window.Create(sf::VideoMode(800, 512), GAME_TITLE);
				if(m_app->m_stateManager.IsExistedState("GameState"))
				{
					m_app->m_stateManager.SetActiveState("GameState");
					m_app->m_stateManager.ResetActiveState();
				}
				else
					m_app->m_stateManager.AddActiveState(new GameState(m_app));
            }
            break;
		}
	}
	if(appEvent.Type == sf::Event::MouseMoved)
	{
		if(IsContain(MENU_CHOOSE_PLAY, appEvent.MouseMove))
		    m_choicePlayer = MENU_CHOOSE_PLAY;
		else if(IsContain(MENU_CHOOSE_OPTION, appEvent.MouseMove))
			m_choicePlayer = MENU_CHOOSE_OPTION;
		else if(IsContain(MENU_CHOOSE_QUIT, appEvent.MouseMove))
		    m_choicePlayer = MENU_CHOOSE_QUIT;
	}
	if(appEvent.Type == sf::Event::MouseButtonPressed)
    {
        sf::Mouse::Button button = appEvent.MouseButton.Button;
        if(button == sf::Mouse::Left)
        {
            if(m_choicePlayer == MENU_CHOOSE_QUIT)
                m_app->Quit();
            if(m_choicePlayer == MENU_CHOOSE_PLAY)
            {
				m_app->m_window.Create(sf::VideoMode(800, 512), GAME_TITLE);
                if(m_app->m_stateManager.IsExistedState("GameState"))
				{
					m_app->m_stateManager.SetActiveState("GameState");
					m_app->m_stateManager.ResetActiveState();
				}
				else
					m_app->m_stateManager.AddActiveState(new GameState(m_app));
            }
        }
    }
}

void MenuState::Draw()
{
	m_app->m_window.Draw(m_spriteMenu);

	if(m_choicePlayer == MENU_CHOOSE_PLAY)
	{
		m_app->m_window.Draw(m_spriteOption);
		m_app->m_window.Draw(m_spriteQuit);
	}
	else if(m_choicePlayer == MENU_CHOOSE_OPTION)
	{
		m_app->m_window.Draw(m_spritePlay);
		m_app->m_window.Draw(m_spriteQuit);
	}
	else if(m_choicePlayer == MENU_CHOOSE_QUIT)
	{
		m_app->m_window.Draw(m_spritePlay);
		m_app->m_window.Draw(m_spriteOption);
	}
}


void MenuState::CleanUp()
{

	// UnLoad Image
	_imageBackground = NULL;
	m_app->m_ressourceManager.UnloadImage("MENU_Background");

	_imageNotHover = NULL;
	m_app->m_ressourceManager.UnloadImage("MENU_NotHover");

	// UnLoad Music
	m_music = NULL;
	m_app->m_ressourceManager.UnloadMusic("MENU_Music");


}

void MenuState::Pause()
{
	m_music->Pause();
}

void MenuState::Resume()
{
	m_music->Play();
}

// Function MenuState
void MenuState::IncrementChoice(IncrementType type)
{
	if(type == REMOVE)
	{
		int int_choice = static_cast<int>(m_choicePlayer);
		int_choice -=1;

		if(int_choice < 0)
			m_choicePlayer = MENU_CHOOSE_QUIT;
		else
			m_choicePlayer = static_cast<MenuPlayerChoice>(int_choice);
	}
	else if(type == ADD)
	{
		int int_choice = static_cast<int>(m_choicePlayer);
		int_choice += 1;

		if(int_choice > 2)
			m_choicePlayer = MENU_CHOOSE_PLAY;
		else
			m_choicePlayer = static_cast<MenuPlayerChoice>(int_choice);
	}

}

bool MenuState::IsContain(MenuPlayerChoice choice, sf::Event::MouseMoveEvent mouseMove)
{
	//sf::IntRect rectangle(m_spritePlay.GetPosition().x, m_spritePlay.GetPosition().y, m_spritePlay.GetPosition().x + m_spritePlay.GetSize().x, m_spritePlay.GetPosition().y + m_spritePlay.GetSize().y);

	if(choice == MENU_CHOOSE_PLAY)
	{
		float x = m_spritePlay.GetPosition().x;
		float y = m_spritePlay.GetPosition().y;
		float w = x + m_spritePlay.GetSize().x;
		float h = y + m_spritePlay.GetSize().y;

		sf::FloatRect rect(x, y, w, h);

		return rect.Contains(static_cast<float>(mouseMove.X), static_cast<float>(mouseMove.Y));
	}
	else if(choice == MENU_CHOOSE_OPTION)
	{
		float x = m_spriteOption.GetPosition().x;
		float y = m_spriteOption.GetPosition().y;
		float w = x + m_spriteOption.GetSize().x;
		float h = y + m_spriteOption.GetSize().y;

		sf::FloatRect rect(x, y, w, h);

		return rect.Contains(static_cast<float>(mouseMove.X), static_cast<float>(mouseMove.Y));
	}
	else if(choice == MENU_CHOOSE_QUIT)
	{
		float x = m_spriteQuit.GetPosition().x;
		float y = m_spriteQuit.GetPosition().y;
		float w = x + m_spriteQuit.GetSize().x;
		float h = y + m_spriteQuit.GetSize().y;

		sf::FloatRect rect(x, y, w, h);

		return rect.Contains(static_cast<float>(mouseMove.X), static_cast<float>(mouseMove.Y));
	}
	else
		return false;


}
